<?php
/**
 * [woniu] Copyright (c) 2016/8/18
 * index.ctrl
 * 首页控制器
 */
defined('IN_IA') or exit('Access Denied');
session_start();
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$pagetitle = !empty($_W['_config']['sname']) ? $_W['_config']['sname'] : '活动首页';
load()->model('pay');
if ($op!='success'){
    //不登录可以访问地址
    if ($op=='task'||$op=='testApp'||$op=='display'||$op=='my'||$op=='detail'||$op=='activity'||$op=='grade'||$op=='photoshow'||$op=='login1'||$op=='login'){
    }else{
        if ($_W['container']!='wechat'){
            aide_message('请用微信打开此链接', '', 'error', '点确定返回');
        }
    }
}
if (empty($_W['fans']['nickname'])){
    $_W['fans'] = getInfo();
}
//首页逻辑
if($op =='display'){
    if(empty($_W['member']['mobile'])&&$_SESSION['status']=='login'){
        $pagetitle = '绑定手机';
        include $this->template ('aitimt/login3');
        exit;
    }
    $coinlogs=pdo_fetchall("SELECT createtime FROM " . tablename('mc_credits_record') . " WHERE uid='{$_W['member']['uid']}' AND credittype ='credit1' ORDER BY  id DESC limit 1");
    $str=mktime(0,0,0,date('m'),date('d'),date('Y'));
    if ($coinlogs[0]['createtime']<$str){
        //积分变更
        aide_load()->model('credit');
        $credit=1;
        $credit_result = credit_update_credit1($_W['member']['uid'], $credit, "爱计时每日登陆：增加" . $credit . '积分', 2);
    }
    $pagetitle = '爱计时赛事平台';
    //查询category中设置不显示的分类，后台设置的四个分类
	$where = array('enabled'=>0);
	$cateData = Util::getNumData('*', 'aide_category', $where, 'id ASC', 1,1,0);
	$cateUrl=['home/index/photo','home/index/video','home/index/know','home/index/cooperation'];
	//读取幻灯片
	$advlist = pdo_fetchall("SELECT id,link,thumb,advname FROM " . tablename('aide_adv') . " WHERE uniacid = '{$_W['uniacid']}' and enabled=1 ORDER BY displayorder DESC");
   //读取热门推荐
    $recommendlist = pdo_fetchall("SELECT id,title,thumb,starttime FROM " . tablename('aide_activity') . " WHERE recommend = 1 AND uniacid='{$_W['uniacid']}' AND is_show = 1 ORDER BY id DESC");
    //读取所有活动
    $templist = pdo_fetchall("SELECT id,title,thumb,starttime FROM " . tablename('aide_activity') . " WHERE is_show=1 AND uniacid='{$_W['uniacid']}' ORDER BY id DESC");
    $month=date('m');
    //设置底部按月份点击显示的数组
    $activityList=['01'=>[],'02'=>[],'03'=>[],'04'=>[],'05'=>[],'06'=>[],'07'=>[],'08'=>[],'09'=>[],'10'=>[],'11'=>[],'12'=>[],];
    foreach ($templist as $key=>$item){
        $num=substr($item['starttime'],5,2);
        $item['starttime']=substr($item['starttime'],0,10);
        $activityList[$num][$item['id']]=$item;
    }
    $openid=(string)$_W['fans']['openid'];
    //通知栏的显示
    $remind = pdo_fetchall("SELECT id,title FROM " . tablename('aide_know') . " WHERE categoryid=15 AND uniacid='{$_W['uniacid']}' ORDER BY id DESC");

    include $this->template ('aitimt/index');
}
//详情逻辑
if($op=='detail'){
    if ($_W['ispost']){
        //关注逻辑
        $type=(string)$_GPC['type'];
        if ($type=='collect'){
            $openid=(string)$_W['fans']['openid'];
            $activityid=(int)$_GPC['activityid'];
            $data=[
                'uniacid'=>(int)$_W['uniacid'],
                'activityid'=>$activityid,
                'openid'=>$openid,
                'status'=>1,
            ];
            $collectList=pdo_fetchall("SELECT id,status FROM " . tablename('aide_activity_collect') . " WHERE activityid=$activityid AND openid='$openid' ORDER BY  id ASC LIMIT 1");
            if (!empty($collectList)){
                if ($collectList[0]['status']==1){
                    pdo_update('aide_activity_collect',['status'=>0],['id'=>$collectList[0]['id']]);
                    die(json_encode(array('errno' => 0,'msg'=>'您已经取消了关注')));
                    exit;
                }else{
                    pdo_update('aide_activity_collect',['status'=>1],['id'=>$collectList[0]['id']]);
                    die(json_encode(array('errno' => 1,'msg'=>'关注成功')));
                    exit;
                }
            }else{
                pdo_insert('aide_activity_collect',$data);
                $id=pdo_insertid();
                if (!$id){
                    die(json_encode(array('errno' => 0,'msg'=>'关注失败')));
                    exit;
                }else{
                    die(json_encode(array('errno' => 1,'msg'=>'关注成功')));
                    exit;
                }
            }
        }elseif($type=='comment'){
            //添加评论
            $uid=(string)$_W['member']['uid'];
            $activityid=(int)$_GPC['activityid'];
            $parentid=(int)$_GPC['pid'];
            $tid=(int)$_GPC['tid'];
            $time=date('Y-m-d H:i:s');
            $data=[
                'aid'=>$activityid,
                'uid'=>$uid,
                'tid'=>$tid,
                'parentid'=>$parentid,
                'nickname'=>$_W['fans']['nickname'],
                'uniacid'=>$_W['uniacid'],
                'content'=>(string)$_GPC['content'],
                'created_at'=>$time,
                'avatar'=>(string)$_W['fans']['tag']['avatar']
            ];
            pdo_insert('aide_activity_comment',$data);
            $id=pdo_insertid();
            if ($id>0){
                $temp=pdo_fetchall("SELECT cid FROM " . tablename('aide_activity_comment_good') . " WHERE fid=$uid AND status=1 ORDER BY  id ASC");
                $goodList=[];
                foreach ($temp as $value){
                    array_push($goodList,$value['cid']);
                }
                $commentList=pdo_fetchall("SELECT avatar,content,created_at,nickname,uid,id,parentid,good FROM " . tablename('aide_activity_comment') . " WHERE aid=$activityid AND uniacid='{$_W['uniacid']}' ORDER BY id ASC");
                foreach ($commentList as &$item){
                    if (in_array($item['id'],$goodList)){
                        $item['gStatus']=1;
                    }
                }
                $commentList=quote_make_tree($commentList);
                die(json_encode(array('errno' => 1,'msg'=>'评论成功','data'=>$commentList)));
                exit;
            }else{
                die(json_encode(array('errno' => 0,'msg'=>'评论失败')));
                exit;
            }
            //评论点赞
        }elseif($type=='good'){
            $cid=(int)$_GPC['cid'];
            $sum= (int)$_GPC['sum'];
            $fid=(int)$_W['member']['uid'];
            $tid=(int)$_GPC['tid'];
            $data=[
                'cid'=>$cid,
                'fid'=>$fid,
                'tid'=>$tid,
            ];
            $good=pdo_fetchall("SELECT id,status FROM " . tablename('aide_activity_comment_good') . " WHERE cid=$cid AND fid=$fid ORDER BY id DESC limit 1");
            if (empty($good)){
                pdo_insert('aide_activity_comment_good',$data);
                pdo_update('aide_activity_comment',['good'=>$sum+1],['id'=>$cid]);
                die(json_encode(array('errno' => 0,'msg'=>'点赞成功','sum'=>$sum+1)));
            }else{
                if ($good[0]['status']==1){
                    pdo_update('aide_activity_comment_good',['status'=>0],['id'=>$good[0]['id']]);
                    pdo_update('aide_activity_comment',['good'=>$sum-1],['id'=>$cid]);
                    die(json_encode(array('errno' => 1,'msg'=>'取消成功','sum'=>$sum-1)));
                }else{
                    pdo_update('aide_activity_comment_good',['status'=>1],['id'=>$good[0]['id']]);
                    pdo_update('aide_activity_comment',['good'=>$sum+1],['id'=>$cid]);
                    die(json_encode(array('errno' => 0,'msg'=>'点赞成功','sum'=>$sum+1)));
                }

            }
            //定时提醒
        }elseif($type=='task') {
            $openid = (string)$_W['fans']['openid'];
            $aid = (int)$_GPC['aid'];
            $jsStatus = (int)$_GPC['js_status'];
            $jeStatus = (int)$_GPC['je_status'];
            $sumStatus = (int)$_GPC['sum_status'];
            $data = [
                'openid' => $openid,
                'mobile' => $_W['member']['mobile'],
                'aid' => $aid,
                'uniacid'=>$_W['uniacid'],
                'js_status' => $jsStatus,
                'je_status' => $jeStatus,
                'sum_status' => $sumStatus,
            ];
            $task = pdo_fetchall("SELECT id FROM " . tablename('aide_activity_task') . " WHERE aid=$aid AND openid='$openid' ORDER BY id DESC limit 1");
            if (empty($task)) {
                pdo_insert('aide_activity_task', $data);
                die(json_encode(array('errno' => 0, 'msg' => '设置成功')));
            } else {
                pdo_update('aide_activity_task', $data, ['id' => $task[0]['id']]);
                die(json_encode(array('errno' => 0, 'msg' => '设置成功')));
            }
        }
    }
    $pagetitle = '赛事详情';
    $activityid=$_GPC['activityid'];
    $openid=$_W['fans']['openid'];
    $uid=$_W['member']['uid'];
    //获取关注状态
    $collectList=pdo_fetchall("SELECT id,status FROM " . tablename('aide_activity_collect') . " WHERE activityid=$activityid AND openid='$openid' ORDER BY  id ASC");
    //获取评论
    $commentList=pdo_fetchall("SELECT id,avatar,content,created_at,nickname,uid,parentid,good FROM " . tablename('aide_activity_comment') . " WHERE aid=$activityid ORDER BY  id ASC");
    $commentList=quote_make_tree($commentList);
    //获取评论点赞状态
    $temp=pdo_fetchall("SELECT cid FROM " . tablename('aide_activity_comment_good') . " WHERE fid=$uid AND status=1 ORDER BY  id DESC");
    $goodList=[];
    foreach ($temp as $value){
        array_push($goodList,$value['cid']);
    }
    //获取提醒状态
    $task =pdo_fetchall("SELECT js_status,je_status,sum_status FROM " . tablename('aide_activity_task') . " WHERE aid=$activityid AND openid='$openid' ORDER BY id ASC");
    //获取当前活动信息
    $adata=pdo_fetchall("SELECT id,title,thumb,starttime,joinstime,joinetime,address,aprice,detail,is_show,atlas FROM " . tablename('aide_activity') . " WHERE id=$activityid  ORDER BY  id DESC LIMIT 1");
    $activity=$adata[0];
    $atlas=unserialize($activity['atlas']);
    $proList=pdo_fetchall("SELECT name,title,pprice,id FROM " . tablename('aide_activity_pro') . " WHERE aid=$activityid  ORDER BY  id ASC");
    include $this->template ('aitimt/detail');
}
//定时提醒
if($op=='task'){
//   对应服务器的php文件位置do.18bang.cn/aideTask.php
//  /localdisk/svn_root/project/match/aideTask.php
    $now=date('Y-m-d H:i:s');
    $temp=pdo_fetchall("SELECT b.*,a.joinstime,a.joinetime,a.quota,a.title,a.msg,a.tense_num FROM ims_aide_activity a JOIN ims_aide_activity_task b ON a.id=b.aid WHERE b.js_status=1 OR je_status=1 OR sum_status=1 ORDER BY b.id DESC");
    foreach ($temp as $item){
        if ($item['joinetime']>$now) {
            $type='';
            $sum='';
            //活动开始时提醒
            if ($item['js_status'] == 1 && $item['joinstime'] < $now) {
                $type='js_status';
            }
            //活动截止报名前一周提醒
            if ($item['je_status'] == 1 && $now > date('Y-m-d H:i:s', strtotime('-1 week', strtotime($item['joinetime'])))) {
                $type='je_status';
            }
            //活动名额紧张提醒
            if ($item['quota']>0){
                if ($item['sum_status'] == 1) {
                    $aid = $item['aid'];
                    $quota = pdo_fetchall("SELECT count(*) num FROM " . tablename('aide_activity_records') . " WHERE activityid=$aid ORDER BY id ASC");
                    if ($item['quota'] - $quota[0]['num'] <= $item['tense_num']) {
                        $type='sum_status';
                        $sum=$item['quota'] - $quota[0]['num'];
                    }
                }
            }
            if ($type==''){
                return;
            }
            $title = $item['title'];
            if ($type=='js_status'){
                $msg = '';
                $msg .= "【报名提醒】尊敬的用户您好,爱计时赛事平台通知您,您关注的赛事<".$title.">已经开始报名了.\n——".date('Y-m-d H:i:s',time())."\n";
                $msg .= "";
            }elseif($type=='je_status'){
                $msg = '';
                $msg .= "【【报名提醒】尊敬的用户您好,爱计时赛事平台通知您,您关注的赛事<".$title.">距离截止报名还剩下一周时间了.\n——".date('Y-m-d H:i:s',time())."\n";
                $msg .= "";
            }elseif ($type='sum_status'){
                $msg = '';
                $msg .= "【报名提醒】尊敬的用户您好,爱计时赛事平台通知您,您关注的赛事<".$title.">当前名额已经剩下不到".$sum."个名额了.\n——".date('Y-m-d H:i:s',time())."\n";
                $msg .= "";
            }
            $openid = $item['openid'];
            $url = 'http://test.18bang.cn/app/index.php?i='.$item['uniacid'].'&c=entry&m=aide_activity&do=home&ac=index&op=detail&activityid='.$item['aid'];
            $arr=[
                'openid'=>$openid,
                'url'=>$url,
                'content'=>$msg,
                'uniacid'=>$item['uniacid'],
            ];
            $result= weixin_success($arr);
            if ($result==1){
                pdo_update('aide_activity_task', [$type => 0], ['id' => $item['id']]);
            }
            if($item['msg']==1){
                $code='';
                if ($type!=null){
                    if ($type=='js_status'){
                        $code='已经开始报名了,';
                    }elseif ($type=='je_status'){
                        $code='已经不足一周了,';
                    }else{
                        $code='名额已经不足'. $item['tense_num'].'个了,';
                    }
                    //发送短信
                    //短信模板id
                    $tCode='SMS_207962865';
                    aide_load()->classs('alisms');
                    $sms = new Sms();
                    //测试模式
                    $result = $sms->send_tips($item['mobile'], $code,$tCode);
                    if ($result){
                        pdo_update('aide_activity_task', [$type => 0], ['id' => $item['id']]);
                    }
                }
            }
        }else{
            pdo_update('aide_activity_task', ['sum_status' => 0,'js_status' => 0,'je_status' => 0], ['id' => $item['id']]);
        }
    }
}
function weixin_success($arr) {
    global $_W;
    $account_api = WeAccount::create($arr['uniacid']);
    $content = "";
    if (is_array($arr['msg'])) {
        foreach ($arr['msg'] as $key => $value) {
            if (!empty($value['title'])) {
                $content .= $value['title'] . ":" . $value['value'] . "\n";
            } else {
                $content .= $value['value'] . "\n";
                if ($key == 0) {
                    $content .= "\n";
                }
            }
        }
    } else {
        $content = $arr['content'];
    }
    if (!empty($arr['url'])) {
        $content .= "<a href='{$arr['url']}'>点击查看详情</a>";
    }
    $result= $account_api -> sendCustomNotice(array("touser" => $arr['openid'], "msgtype" => "text", "text" => array('content' => urlencode($content))));
    //载入日志函数
    load()->func('logging');
//记录文本日志
    logging_run(json_encode($arr));
    return $result;
}
if ($op=='testApp'){
    $url = app_url('home/index/detail') . '&activityid=9';
  $result= task_success('oQbMswqqQMHEjrpYpmHDXe1KH8ls','js_status',$url,'测试','100');
    print_r($result);
}
//确认信息
if($op=='free'||$op=='pay'){
    $pagetitle = '确认信息';
    $rid=(int)$_GPC['recordid'];
    $record= pdo_fetchall("SELECT * FROM " . tablename('aide_activity_records') . " WHERE id=$rid ORDER BY  id DESC  LIMIT 1 ");
    $formdata= pdo_fetchall("SELECT * FROM " . tablename('aide_form_data_common') . " WHERE rid=$rid ORDER BY  rid DESC  LIMIT 1 ");
    $form=$formdata[0];
    $form['cynumber']=$record[0]['cynumber'];
    $activityid=$record[0]['activityid'];
    $proid=$record[0]['pid'];
    $temp=pdo_fetchall("SELECT * FROM " . tablename('aide_form') . " WHERE uniacid='{$_W['uniacid']}' AND activityid=$activityid ORDER BY id ASC");
   $trans=[];
    foreach ($temp as $value){
        $trans[$value['fieldstype']]=$value['title'];
    }
    $adata=pdo_fetchall("SELECT id,title,thumb,starttime,joinetime,address FROM " . tablename('aide_activity') . " WHERE id=$activityid  ORDER BY  id DESC LIMIT 1");
    $activity=$adata[0];
    $pdata=pdo_fetchall("SELECT id,name,title FROM " . tablename('aide_activity_pro') . " WHERE id=$proid  ORDER BY  id ASC LIMIT 1");
    $pro=$pdata[0];
    if($record[0]['price']==0){//免费活动与收费活动的区别跳转
        include $this->template ('aitimt/free');
    } else{
        include $this->template ('aitimt/pay');
        }
}
//报名成功页面
if($op=='success'){
        $pagetitle = '报名成功';
        $rid=(int)$_GPC['recordid'];
        $openid=$_GPC['openid']?(string)$_GPC['openid']:$_W['fans']['openid'];
        $peodata = pdo_fetchall("SELECT realname,gender FROM " . tablename('aide_form_data_common') . " WHERE rid=$rid ORDER BY  rid DESC  LIMIT 1 ");
        $people=$peodata[0];
        $record= pdo_fetchall("SELECT id,pid,activityid,entrystatus,payprice FROM " . tablename('aide_activity_records') . " WHERE id=$rid ORDER BY  id DESC  LIMIT 1 ");
        $pid=$record[0]['pid'];
        $activity = model_activity::getSingleActivity($record[0]['activityid'], '*');
        $pro=pdo_fetchall("SELECT id,no_limit,sum FROM " . tablename('aide_activity_pro') . " WHERE id=$pid ORDER BY id DESC  LIMIT 1 ");
        if ($record[0]['entrystatus']==0){
            if ($record[0]['payprice']>0){
                aide_load()->model('credit');
                $credit=5;
                $credit_result = credit_update_credit1($_W['member']['uid'], $credit, "爱计时报名：增加" . $credit . '积分', 1);
            }
            if (!$activity['hasreview']) {//活动不开启审核发送通知
                $msg = '';
                $msg .= "【报名提醒】尊敬的用户您好,爱计时赛事平台通知您,您已经成功报名<".$title.">.\n——".date('Y-m-d H:i:s',time())."\n";
                $msg .= "";
                $url = 'http://test.18bang.cn/app/index.php?i='.$activity['uniacid'].'&c=entry&m=aide_activity&do=home&ac=index&op=myapply';
                $postArr=[
                    'openid'=>$openid,
                    'url'=>$url,
                    'content'=>$msg,
                    'uniacid'=>$activity['uniacid'],
                ];
                //公众号通知
            weixin_success($postArr);
            if($activity['smsswitch']){//短信通知
            $smsparams=array(
            'product' => $_W['_config']['sname'],
            'item'    => $activity['title'],
            'name'    => $data['realname'],
            'timestr' => date('m月d日 H:i',strtotime($activity['starttime'])),
            'idcode'  => $data['hexiaoma']
            );
            $template_id = empty($activity['smsnotify']) ? $_W['_config']['sms_notify'] : $activity['smsnotify'];
            sendSMS($data['mobile'], $smsparams, $template_id, $_W['_config']['sms_type']);
            }
            }else{
            //审核通知
            message::join_review($_W['openid'], $activity, 2, app_url('home/index/myapply'));
            }
        }
        if ($pro[0]['no_limit']==0){//是否限制报名人数参数
           numberrule($record[0]);
           pdo_update('aide_activity_records',['entrystatus'=>1],['id'=>$rid]);
           include $this->template ('aitimt/success');
       } else{
           if ($pro[0]['sum']<=0){
               aide_message('当前活动项目名额不足', '', 'error', '点确定返回');
               exit;
           } else{
               $entrystatus=$record[0]['entrystatus'];
               if ($entrystatus==0){
                   $sum=(int)$pro[0]['sum']-1;
                   pdo_update('aide_activity_records',['entrystatus'=>1],['id'=>$rid]);
                   pdo_update('aide_activity_pro',['sum'=>$sum],['id'=>$pro[0]['id']]);
                   numberrule($record[0]);
               }
               include $this->template ('aitimt/success');
           }
       }

}
//支付,支付成功处理库存
if ($op=='topay'){
    $rid=(int)$_GPC['recordid'];
    $recordsdata = pdo_fetchall("SELECT id,pid,price,orderno,uid FROM " . tablename('aide_activity_records') . " WHERE id=$rid ORDER BY  id DESC  LIMIT 1 ");
    $record=$recordsdata[0];
    $pid=$record['pid'];
    $pro=pdo_fetchall("SELECT * FROM " . tablename('aide_activity_pro') . " WHERE id=$pid ORDER BY  id DESC  LIMIT 1 ");
    if ($pro[0]['no_limit']!=0){//是否限制报名人数参数
        if ($pro[0]['sum']<=0){
            aide_message('当前活动项目名额不足', '', 'error', '点确定返回');
            exit();
        }
    }
    //获取用户要充值的金额数
    $fee = $record['price'];
    if($fee <= 0) {
        message('支付错误, 金额小于0');
    }
    //构造支付请求中的参数
    $params = array(
        'tid' => $record['orderno'],      //充值模块中的订单号，此号码用于业务模块中区分订单，交易的识别码
        'ordersn' => $record['orderno'],  //收银台中显示的订单号
        'title' => '活动支付金额',          //收银台中显示的标题
        'fee' => $record['price'],      //收银台中显示需要支付的金额,只能大于 0
        'user' => $record['uid'],     //付款用户, 付款的用户名(选填项)
    );
    //调用pay方法
    $this->pay($params);
}
//我的报名
if ($op=='myapply'){
    $pagetitle = '我的报名';
    if ($_W['container']!='wechat'){
        aide_message('请用微信打开此链接查看', '', 'error', '点确定返回');
    }
    include $this->template ('aitimt/myapply');
}
//我的页面
if ($op=='my'){
    $pagetitle = '我的';
    $uid=(string)$_W['member']['uid'];
    $peodata = pdo_fetchall("SELECT uid,avatar,mobile,nickname FROM " . tablename('mc_members') . " WHERE uid=$uid ORDER BY uid ASC  LIMIT 1 ");
    $people=$peodata[0];
    include $this->template ('aitimt/my');
}
//我的消息
if ($op=='myremind'){
    $pagetitle = '个人中心-我的消息';
    $cateList=['通知','评论我的','赞我的'];
    if ($_W['isajax']){
        $typeid=(int)$_GPC['status'];
        $openid=$_W['fans']['openid'];
        $uid=$_W['member']['uid'];
        if ($typeid==1){
            //获取通知
            $remind = pdo_fetchall("SELECT a.* FROM ims_aide_activity_remind a JOIN ims_aide_activity_records b ON a.uniacid=b.uniacid WHERE b.uniacid='{$_W['uniacid']}' AND a.fans_group=1 OR a.fans_group=3 AND b.uniacid='{$_W['uniacid']}' AND b.openid='$openid' GROUP BY a.id ORDER BY a.id DESC");
            $data['list']=$remind;
            die(json_encode($data));
            exit;
        }elseif($typeid==2){
            //获取评论
            $comment= pdo_fetchall("SELECT a.id,a.content,a.created_at,a.nickname,a.avatar,a.uid,a.parentid,a.aid,b.thumb FROM ims_aide_activity_comment a JOIN ims_aide_activity b ON a.aid=b.id WHERE a.tid=$uid ORDER BY a.id DESC");
            $data['list']=$comment;
            die(json_encode($data));
            exit;
        }elseif ($typeid==3){
            //获取点赞
            $data['list']=pdo_fetchall("SELECT good,created_at,content,nickname FROM " . tablename('aide_activity_comment') . " WHERE uid=$uid AND good>0 ORDER BY id ASC");
            die(json_encode($data));
            exit;
        }

    }
    include $this->template ('aitimt/myremind');

}
//产品视频
if ($op=='test'){
    header('Content-Type: text/html;charset=utf-8');
    header('Access-Control-Allow-Origin:http://do.18bang.cn'); // *代表允许任何网址请求
    header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
    header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
    header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
    $category = pdo_fetchall("SELECT id,parentid,name,file_path FROM " . tablename('aide_extend_type') . " WHERE id>0 ORDER BY  id ASC");
    $arr=quote_make_tree($category);
    die(json_encode($arr));exit();
}
if ($op=='test1'){
    $category = pdo_fetchall("SELECT id,parentid,name,file_path FROM " . tablename('aide_extend_type') . " WHERE id>0 ORDER BY  id ASC");
    $arr=quote_make_tree($category);
    die(json_encode($arr));exit();
    include $this->template ('aitimt/test');
}

function quote_make_tree($list, $pk = 'id', $pid = 'parentid',$child = 'child', $root = 0)
{
    $tree = $packData = [];
    foreach ($list as $data) {
        $packData[$data[$pk]] = $data;
    }
    foreach ($packData as $key =>$val) {
        if ($val[$pid] == $root) {//代表跟节点
            $tree[] = & $packData[$key];
        } else {
            //找到其父类
            $packData[$val[$pid]][$child][] = & $packData[$key];
        }
    }
    return $tree;
}
//我的信息
if ($op=='mydata'){
    $pagetitle = '个人中心-我的信息';
    if ($_W['ispost']){
        $key= $_GPC['type'];
        $value=$_GPC[$key];
        $formdata=[
            $key => (string)$value,
        ];
       $message= pdo_update('mc_members',$formdata,['uid'=>(int)$_GPC['uid']]);
       if ($message==1){
           die(json_encode(array('type' => 'success','message'=>'修改成功')));
       }else{
           die(json_encode(array('type' => 'fail','message'=>'修改失败')));
       }
    }
    $uid=(string)$_W['member']['uid'];
    $peodata = pdo_fetchall("SELECT uid,avatar,realname,gender,mobile,email,residecity,nickname FROM " . tablename('mc_members') . " WHERE uid=$uid ORDER BY uid ASC  LIMIT 1 ");
    $people=$peodata[0];
    if (empty($people['avatar'])){
        $people['avatar']=$_W['fans']['avatar'];
    }
    include $this->template ('aitimt/mydata');
}
//我的报名消息获取
if($op =='ajax'){
    $pindex = max(1, intval($_GPC['page']));
    $psize = max(1, intval($_GPC['pagesize']));
    $condition = " B.uniacid = '{$_W['uniacid']}' and A.id = activityid and openid = '{$_W['openid']}'";//{$_W['openid']}o7CHdvlvTW37Kg51Hmycd0y9EmUk
    switch($_GPC['status']){
        case 0:$condition .=" and status = 0";break;
        case 1:$condition .=" and (status = 1 or status = 2)";break;
        case 3:$condition .=" and status = 3 ";break;
        case 5:$condition .=" and status = 5";break;
        case 7:$condition .=" and status = 7";break;
        default:'';
    }

    $field  = "B.id, activityid, teamnum, jointime, A.title, A.merchantid, starttime, endtime, joinstime, joinetime, B.price,B.cynumber,B.entrystatus, B.aprice, paytype, payprice, thumb, orderno ,status, ishexiao, hasoption, optionid, hascancel,pid, B.review";
    $inner  = tablename ('aide_activity') . " A, " . tablename ('aide_activity_records') . " B";
    $list = pdo_fetchall ("SELECT $field FROM " . $inner . " WHERE $condition ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $arrprolist = pdo_fetchall("SELECT * FROM " . tablename('aide_activity_pro') . " WHERE uniacid='{$_W['uniacid']}' ORDER BY  id DESC ");
    foreach ($arrprolist as $arrVal){
        $arrprolist[$arrVal['id']]=$arrVal;
        unset($arrprolist[0]);
    }
    $arrformlist = pdo_fetchall("SELECT * FROM " . tablename('aide_form_data_common') . " WHERE uniacid='{$_W['uniacid']}' ORDER BY  rid DESC ");
    foreach ($arrformlist as $arrValue){
        $arrformlist[$arrValue['rid']]=$arrValue;
        unset($arrformlist[0]);
    }
    foreach ($list as &$s) {
        $pro=$arrprolist[$s['pid']];
        $form=$arrformlist[$s['id']];
        $s['pname']=$pro['name'];
        $s['fid']=$form['rid'];
        if ($pro['pprice']==0){
            $s['pprice']='免费';
        }else{
            $s['pprice']=$pro['pprice'].'元';
        }
    }
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . $inner . " WHERE $condition");
    //array_merge数组拼接
    $data['list']=$list;
    $data['tpage']=(empty($psize) || $psize < 0) ? 1 : ceil($total / $psize);
    $data['total']=$total;
    die(json_encode($data));
    exit;
}
if($_W['isajax'] && $op == 'getparams'){
	$activityid = intval($_GPC['activityid']);
	$activity = model_activity::getSingleActivity($activityid, '*');
	//读取规格名额
	if($activity['hasoption']){
		$aprice['max'] = pdo_fetchcolumn("SELECT max(aprice) FROM " . tablename('aide_spec_option') . " WHERE aprice > 0 and activityid = " .$activity['id']);
		$aprice['min'] = pdo_fetchcolumn("SELECT min(aprice) FROM " . tablename('aide_spec_option') . " WHERE aprice > 0 and activityid = " .$activity['id']);
		//读取规格总名额，总虚拟人数
		$opt['quota'] = pdo_fetchcolumn("SELECT SUM(quota) FROM " . tablename('aide_spec_option') . " WHERE activityid = " .$activity['id']);
		$opt['falsenum'] = pdo_fetchcolumn("SELECT SUM(falsenum) FROM " . tablename('aide_spec_option') . " WHERE activityid = ".$activity['id']);
		$activity['quota'] = $opt['quota'] ? $opt['quota'] : $activity['quota'];
		$activity['falsenum'] = $opt['falsenum'] ? $opt['falsenum'] : 0;
	}
	$aprice['min'] = !empty($aprice['min']) && $aprice['max'] >= $aprice['min']?$aprice['min']:0;
	$params = array(
		"joinnum" => model_records::getJoinNum($activityid) + $activity['falsenum'],
		"quota"  => $activity['quota'],
		"aprice"  => $activity['aprice'],
		"minaprice"  => $aprice['min']
	);
	die(json_encode($params));
	exit;
}
//赛事页面
if ($op=='activity'){
    $pagetitle = '赛事';
    $item = array('time'=>'时间','city'=>'城市','type'=>'类型','status'=>'状态');
    $time = array('00'=>'全部','01'=>'1月','02'=>'2月','03'=>'3月','04'=>'4月','05'=>'5月','06'=>'6月','07'=>'7月','08'=>'8月','09'=>'9月','10'=>'10月','11'=>'11月','12'=>'12月');
    $city = array('all'=>'全部','recommend'=>'热门','in'=>'国内','out'=>'国外');
    $recommend = array('北京','上海','成都','广州','深圳','南京','贵州','杭州');
    $in = array('北京','上海','天津','重庆','河北','山西','辽宁','吉林','江苏','浙江','安徽','福建','江西','山东','河南','湖北','湖南',
        '广东','广西','海南', '四川','贵州','云南','西藏','陕西','甘肃','宁夏','青海','新疆','内蒙古','黑龙江','香港','澳门','台湾');
    $out = array('泰国','新加坡','意大利','日本','西班牙','以色列','阿曼','美国','印度','法国','韩国','德国');
    $status = array('all'=>'全部','will'=>'即将报名','now'=>'报名中','end'=>'报名结束','my'=>'我关注的');
    $cateData =pdo_fetchall("SELECT id,name FROM " . tablename('aide_category') . " WHERE enabled=1 AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC");
    $type=[];
    foreach ($cateData as $val) {
        $type[$val['id']]=$val['name'];
    }
    if (empty($_GPC['search'])){
        $activityList = pdo_fetchall("SELECT id,thumb,is_show,endtime,starttime,joinstime,joinetime,title,address,aprice FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND is_show=1 ORDER BY  id DESC");
    }else{
        $activityList=search($_GPC['search'],$_GPC['value']);
    }

    include $this->template ('aitimt/activity');
}
//赛事查询
if ($op=='search'){
    $search=(string)$_GPC['search'];
    if ($search=='query') {
        //报名查询
        $value1 = $_GPC['value'];
        $aid = $_GPC['aid'];
        $recordList = pdo_fetchall("SELECT a.status,a.realname,a.jointime,b.name FROM ims_aide_activity_records a JOIN ims_aide_activity_pro b ON a.pid=b.id WHERE a.activityid=$aid AND a.mobile=$value1 OR a.idcard=$value1 group by a.id DESC limit 1;");
        die(json_encode($recordList));
    }
    $value=(string)$_GPC['value'];
    $activityList=search($search,$value);
    die(json_encode($activityList));
    exit;
}
//报名查询页面
if ($op=='query'){
    $pagetitle = '报名查询';
    $activityid=(int)$_GPC['activityid'];
    $genderMes = pdo_fetchall("SELECT a.gender,count(a.gender) gsum FROM ims_aide_form_data_common a JOIN ims_aide_activity_records b ON a.rid=b.id WHERE b.activityid=$activityid AND a.gender>0 group by a.gender;");
    $sum = pdo_fetchall("SELECT count(id) psum FROM " . tablename('aide_activity_records') . " WHERE uniacid='{$_W['uniacid']}' AND activityid=$activityid ORDER BY id DESC");
   $sum=$sum[0]['psum'];
    $conMes = pdo_fetchall("SELECT a.idcard FROM ims_aide_form_data_common a JOIN ims_aide_activity_records b ON a.rid=b.id WHERE b.activityid=$activityid;");
    $arrTemp=$arrWen=$arrNum=[];
    $arrCon=['白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','水瓶座','双鱼座'];
    foreach ($conMes as $item){
        if ($item['idcard']!=null){
            $name=tt($item['idcard']);
            unset($arrCon[$item['idcard']]);
            array_push($arrTemp,$name);
        }
    }
    $arrTemp=(array_count_values($arrTemp));
    $arrWen=array_keys($arrTemp);
    $arrNum=array_values($arrTemp);
    if ($arrCon!=[]){
        foreach ($arrCon as $value){
            if (!in_array($value,$arrWen)){
                array_push($arrWen,$value);
                array_push($arrNum,0);
            }

        }
    }
    $arrWen=json_encode($arrWen);
    $arrNum=json_encode($arrNum);
    if (empty($genderMes)){
        $arrGender=[0,0];
    }
    if (empty($genderMes[1])){
         if ($genderMes[0]['gender']==1){
             $arrGender=[100,0,];
         }else{
             $arrGender=[0, 100,];
         }
    }
    $arrGender=[$genderMes[0]['gsum'], $genderMes[1]['gsum']];

    include $this->template ('aitimt/query');
}
function tt($idcard){
    $month=(string)substr($idcard,10,2);
    $day=(int)substr($idcard,12,2);
    $name='';
    switch ($month) {
        case '01':
            if ($day<=31&&$day>=20) {
                $name='水瓶座';
            }else{
                $name='摩羯座';
            }
            break;
        case '02':
            if ($day<=18&&$day>=1) {
                $name='水瓶座';
            }else{
                $name='双鱼座';
            }
            break;
        case '03':
            if ($day<=20&&$day>=1) {
                $name='双鱼座';
            }else{
                $name='白羊座';
            }
            break;
        case '04':
            if ($day<=19&&$day>=1) {
                $name='白羊座';
            }else{
                $name='金牛座';
            }
            break;
        case '05':
            if ($day<=20&&$day>=1) {
                $name='金牛座';
            }else{
                $name='双子座';
            }
            break;
        case '06':
            if ($day<=21&&$day>=1) {
                $name='双子座';
            }else{
                $name='巨蟹座';
            }
            break;
        case '07':
            if ($day<=22&&$day>=1) {
                $name='巨蟹座';
            }else{
                $name='狮子座';
            }
            break;
        case '08':
            if ($day<=22&&$day>=1) {
                $name='狮子座';
            }else{
                $name='处女座';
            }
            break;
        case '09':
            if ($day<=23&&$day>=1) {
                $name='处女座';
            }else{
                $name='天秤座';
            }
            break;
        case '10':
            if ($day<=23&&$day>=1) {
                $name='天秤座';
            }else{
                $name='天蝎座';
            }
            break;
        case '11':
            if ($day<=22&&$day>=1) {
                $name='天蝎座';
            }else{
                $name='射手座';
            }
            break;
        case '12':
            if ($day<=23&&$day>=1) {
                $name='射手座';
            }else{
                $name='摩羯座';
            }
            break;
        default:
            echo 'error';break;
    }
    return $name;
}
//成绩页面
if ($op=='grade'||$op=='mygrade'){
    $pagetitle = '成绩';
    //区分两个后台的url
    $url1='http://matchtest.aidemed.com/api/source/getDomainList';
    $arrUrl=json_decode(curl_get($url1),true);
    $url1=$arrUrl[0].'/api/source/getMatchList';
    $tempList1=json_decode(curl_get($url1),true);
    $url2=$arrUrl[1].'/api/source/getMatchList';
    $tempList2=json_decode(curl_get($url2),true);
    $tempList=array_merge($tempList1,$tempList2);
    $count=count($tempList1);
    $activityList=[];
    foreach ($tempList as $key=>&$item){
        $time=date('Y-m-s', $item['matchtime']);
        $num=substr($time,0,4);
        $item['time']=$time;
        if ($key<$count){
            $item['url']=$arrUrl[0];
        }else{
            $item['url']=$arrUrl[1];
        }
        $activityList[$num][$item['raceid']]=$item;
    }
    if ($op=='mygrade'){
        include $this->template ('aitimt/mygrade');exit();

    }else{
        include $this->template ('aitimt/grade');

    }
}
//成绩搜索页面
if ($op=='gradesearch'){
    $pagetitle = '成绩查询';
    //区分两个后台的url
    $url1='http://matchtest.aidemed.com/api/source/getDomainList';
    $arrUrl=json_decode(curl_get($url1),true);
    if($_W['ispost']){
        if ((string)$_GPC['url']==$arrUrl[0]){
            $url=$arrUrl[0].'/api/source/getTranscript';
        }else{
            $url=$arrUrl[1].'/api/source/getTranscript';
        }
        $number=(string)$_GPC['raceNum'];
        $raceid=(int)$_GPC['raceid'];
        $arr=['raceid'=>$raceid,'raceNum'=>$number];
        $message=json_decode(curl_post($url,$arr),true);
        if ($message==[]){
            die(json_encode(['errno'=>1]));
        }else{
            die(json_encode(['errno'=>0]));
        }
    }
    $raceid=(int)$_GPC['raceid'];
    //前端传递的参数
    $url2=(string)$_GPC['url'];
    $message=[];
    if ((string)$_GPC['url']==$arrUrl[0]){
        $url=$arrUrl[0].'/api/source/getMatchList';
    }else{
        $url=$arrUrl[1].'/api/source/getMatchList';
    }
    $templist=json_decode(curl_get($url),true);
    foreach ($templist as $item){
        if ($item['raceid']==$raceid){
            $message=$item;
            break;
        }
    }
    $message['time']=date('Y-m-s h:i:s', $message['matchtime']);
    include $this->template ('aitimt/gradesearch');
}
//成绩详情页面
if ($op=='grademess'){
    $pagetitle = '成绩详情';
    //区分两个后台的url
    $url1='http://matchtest.aidemed.com/api/source/getDomainList';
    $arrUrl=json_decode(curl_get($url1),true);
    if ((string)$_GPC['url']==$arrUrl[0]){
        $url=$arrUrl[0].'/api/source/getTranscript';
    }else{
        $url=$arrUrl[1].'/api/source/getTranscript';
    }
    $number=(string)$_GPC['raceNum'];
    $raceid=(int)$_GPC['raceid'];
    $arr=['raceid'=>$raceid,'raceNum'=>$number];
    $message=json_decode(curl_post($url,$arr),true);
    include $this->template ('aitimt/grademess');
}
//我的关注页面
if ($op=='mycollect'){
    $pagetitle = '我的关注';
    $openid=(string)$_W['fans']['openid'];
    $activityList = pdo_fetchall("SELECT id,thumb,is_show,endtime,starttime,joinstime,joinetime,title,address,aprice FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND id IN (SELECT activityid FROM `ims_aide_activity_collect` WHERE status=1 AND openid='$openid' AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC)");
    include $this->template ('aitimt/mycollect');
}
//照片页面
if ($op=='photo'){
    $pagetitle = '照片';
    //读取幻灯片
    $advlist = pdo_fetchall("SELECT id,link,thumb,advname FROM " . tablename('aide_adv') . " WHERE uniacid = '{$_W['uniacid']}' and enabled=1 ORDER BY displayorder DESC");
    $photoList=pdo_fetchall("SELECT id,raceid,atlas FROM " . tablename('aide_activity_photo') . " WHERE id>0 ORDER BY id DESC");
    $url='http://video.aidemed.com/api/data/raceList';
    $activityList=[];
    $templist=json_decode(curl_get($url),true);
    foreach ($templist as $key=>$item){
        $time=date('Y-m-s', $item['matchtime']);
        $item['time']=$time;
        $activityList[$item['raceid']]=$item;
    }
    include $this->template ('aitimt/photo');
}
//照片页面
if ($op=='photosearch'){
    $pagetitle = '照片';
    $raceid=$_GPC['raceid'];
    $racNum=$_GPC['racNum'];
    $photo=pdo_fetchall("SELECT id,link,raceid,atlas,share_img,share_link,share_title,share_desc FROM " . tablename('aide_activity_photo') . " WHERE raceid=$raceid ORDER BY id DESC limit 1");
    $photo=$photo[0];
    $url='http://video.aidemed.com/api/data/raceList';
    $activityList=json_decode(curl_get($url),true);
    if($_W['ispost']){
        $url='http://video.aidemed.com/api/data/getImgageList';
        $arr=['raceid'=>(int)$_GPC['raceid'],'number'=>(string)$_GPC['number']];
        $photoList=json_decode(curl_post($url,$arr),true);
        die(json_encode($photoList));
    }
    include $this->template ('aitimt/photosearch');
}
function curl_get($url){
$header = array(
    'Accept: application/json',
);
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 超时设置,以秒为单位
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);
    // 超时设置，以毫秒为单位
//     curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);
    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        curl_close($curl);
        return $data;

    }
}
// $postdata 是传输的数据，数组格式
function curl_post( $url, $postdata ) {
    $header = array(
        'Accept: application/json',
    );
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 超时设置
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    //执行命令
    $data = curl_exec($curl);
    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        curl_close($curl);
        return $data;
    }
}
//照片显示页面
if ($op=='photoshow'){
    $pagetitle = '照片显示';
    $raceid=$_GPC['raceid'];
    $photo=pdo_fetchall("SELECT id,raceid,atlas,share_img,share_link,share_title,share_desc FROM " . tablename('aide_activity_photo') . " WHERE raceid=$raceid ORDER BY id DESC limit 1");
    $photo=$photo[0];
    $url='http://video.aidemed.com/api/data/getImgageList';
    $arr=['raceid'=>(int)$_GPC['raceid'],'number'=>(string)$_GPC['number']];
    $photoList=json_decode(curl_post($url,$arr),true);
    if ($_W['ispost']) {
        $uid = (int)$_W['member']['uid'];
        $img_id = (int)$_GPC['img_id'];
        $img_src = (string)$_GPC['img_src'];
        $type = (string)$_GPC['type'];
        $status=(string)$_GPC['status'];
        $data = [
            'uniacid' => (int)$_W['uniacid'],
            'uid' => $uid,
            'img_id' => $img_id,
            'img_src' => $img_src,
                $type=>1,
        ];
        $collectList = pdo_fetchall("SELECT id,collect,good,g_status,c_status,report,r_status FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND img_src='$img_src' ORDER BY  id ASC LIMIT 1");
        if (!empty($collectList)) {
            if ($type=='collect'){
                if ($collectList[0]['collect'] == 1) {
                    pdo_update('aide_activity_photo_collect', ['collect' => 0], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                } else {
                    pdo_update('aide_activity_photo_collect', ['collect' => 1], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 1, 'msg' => '收藏成功')));
                }
            }elseif ($type=='good'){
                if ($collectList[0]['good'] == 1) {
                    pdo_update('aide_activity_photo_collect', ['good' => 0], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                } else {
                    pdo_update('aide_activity_photo_collect', ['good' => 1], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 1, 'msg' => '点赞成功')));
                }
            }elseif ($type=='report'){
                if ($collectList[0]['report'] == 1) {
                    pdo_update('aide_activity_photo_collect', ['report' => 0], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                } else {
                    pdo_update('aide_activity_photo_collect', ['report' => 1], ['id' => $collectList[0]['id']]);
                    die(json_encode(array('errno' => 1, 'msg' => '收藏成功')));
                }
                //photodown页面处理
            }elseif($type='index'){
                if ($status=='collect'){
                    pdo_update('aide_activity_photo_collect', ['c_status'=>0], ['uid'=>$uid]);
                    if ($collectList[0]['c_status'] == 1) {
                        pdo_update('aide_activity_photo_collect', ['c_status' => 0], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                    } else {
                        pdo_update('aide_activity_photo_collect', ['c_status' => 1], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 1, 'msg' => '设置成功')));
                    }
                }elseif ($status=='good'){
                    pdo_update('aide_activity_photo_collect', ['g_status'=>0], ['uid'=>$uid]);
                    if ($collectList[0]['g_status'] == 1) {
                        pdo_update('aide_activity_photo_collect', ['g_status' => 0], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                    } else {
                        pdo_update('aide_activity_photo_collect', ['g_status' => 1], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 1, 'msg' => '设置成功')));
                    }
                }elseif ($status=='report'){
                    pdo_update('aide_activity_photo_collect', ['r_status'=>0], ['uid'=>$uid]);
                    if ($collectList[0]['r_status'] == 1) {
                        pdo_update('aide_activity_photo_collect', ['r_status' => 0], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 0, 'msg' => '取消成功')));
                    } else {
                        pdo_update('aide_activity_photo_collect', ['r_status' => 1], ['id' => $collectList[0]['id']]);
                        die(json_encode(array('errno' => 1, 'msg' => '设置成功')));
                    }
                }

            }

        } else {
            pdo_insert('aide_activity_photo_collect', $data);
            $id = pdo_insertid();
            $arr=['collect'=>'收藏','good'=>'点赞','report'=>'收藏'];
            if (!$id) {
                die(json_encode(array('errno' => 0, 'msg' => $arr[$type].'失败')));
            } else {
                die(json_encode(array('errno' => 1, 'msg' => $arr[$type].'成功')));
            }
        }
    }
    include $this->template ('aitimt/photoshow');
}
//照片状态查询
if ($op=='photostatus'){
    $uid = (int)$_W['member']['uid'];
    $img_src = (string)$_GPC['img_src'];
    $collectList = pdo_fetchall("SELECT id,collect,good,g_status,c_status,report,r_status FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND img_src='$img_src' ORDER BY id ASC LIMIT 1");
    if (!empty($collectList)) {
        die(json_encode(['collect'=>$collectList[0]['collect'],'good'=>$collectList[0]['good'],'g_status'=>$collectList[0]['g_status'],'c_status'=>$collectList[0]['c_status'],'report'=>$collectList[0]['report'],'r_status'=>$collectList[0]['r_status']]));
    }else{
        die(json_encode(['collect'=>0,'good'=>0,'g_status'=>0,'c_status'=>0,'report'=>0,'r_status'=>0]));

    }
}
//视频页面
if ($op=='video'){
    if ($_W['ispost']){
        $id=(int)$_GPC['videoid'];
        $video=pdo_fetchall("SELECT hits FROM " . tablename('aide_activity_video') . " WHERE id=$id ORDER BY id DESC LIMIT 1");
            $data=[
                'hits'=>$video[0]['hits']+1,
                'updated_at'=>$time,
            ];
            pdo_update('aide_activity_video',$data,['id'=>$id]);
        die(json_encode(array('errno' => 1,'hits'=>$data['hits'],)));
            exit;
        }
    $pagetitle = '视频';
    $video=pdo_fetchall("SELECT recom,hits,title,file_path,id,img FROM " . tablename('aide_activity_video') . " WHERE uniacid='{$_W['uniacid']}' ORDER BY id DESC");
    include $this->template ('aitimt/video2');
}
//知道页面
if ($op=='know'){
    $pagetitle = '知道';
    $cateList=pdo_fetchall("SELECT id,name FROM " . tablename('aide_category') . " WHERE parentid=5 ORDER BY id ASC LIMIT 4");
    if ($_W['isajax']){
        $pagetitle = '文章';
        $catagoryid=(int)$_GPC['status'];
        $knowList=pdo_fetchall("SELECT id,image,title,intro,updated_at FROM " . tablename('aide_know') . " WHERE categoryid=$catagoryid AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC");
        foreach ($knowList as &$value){
            $value['updated_at']=substr($value['updated_at'],0,10);
        }
        $data['list']=$knowList;
        die(json_encode($data));
        exit;
    }
    if(!empty($_GPC['type'])){
        $id=(int)$_GPC['id'];
        if ($_GPC['type']=='know'){
            $temp=pdo_fetchall("SELECT id,title,intro,content FROM " . tablename('aide_know') . " WHERE id=$id ORDER BY  id DESC LIMIT 1");
            $article=$temp[0];
            $pagetitle = $article['title'];
            include $this->template ('aitimt/article');
        }elseif ($_GPC['type']=='intro'){
            $temp=pdo_fetchall("SELECT id,title,intro,content FROM " . tablename('aide_know') . " WHERE id=$id ORDER BY  id DESC LIMIT 1");
            $article=$temp[0];
            $pagetitle = $article['title'];
            include $this->template ('aitimt/article');
        }elseif ($_GPC['type']=='remind'){
            $temp=pdo_fetchall("SELECT * FROM " . tablename('aide_activity_remind') . " WHERE id=$id ORDER BY  id DESC LIMIT 1");
            $article=$temp[0];
            $pagetitle = $article['title'];
            include $this->template ('aitimt/article');
        }

    }else{
        include $this->template ('aitimt/know');
    }
}
//合作页面
if ($op=='cooperation'){
    $pagetitle = '合作';
    $article=pdo_fetchall("SELECT id FROM " . tablename('aide_know') . " WHERE categoryid=14 ORDER BY  id DESC LIMIT 1");
    $introId=$article[0]['id'];
    include $this->template ('aitimt/cooperation');
}
//合作咨询页面
if ($op=='cooper'){
    $pagetitle = '合作咨询';
    if ($_W['ispost']){
        $form = $_GPC['cooper'];
        $time=date('Y-m-d H:i:s');
        $data = [
            'name' => (string)$form['name'],
            'uniacid' => (int)$_W['uniacid'],
            'mobile' => (string)$form['mobile'],
            'email' => (string)$form['email'],
            'type' => (string)$form['type'],
            'content' => (string)$form['content'],
            'created_at'=>$time
        ];
        pdo_insert('aide_cooper', $data);
        $fid = pdo_insertid();
        if (!empty($fid)) {
            die(json_encode(array('errno' => 0, 'errmsg' => '提交成功')));
            exit;
        }else{
            die(json_encode(array('errno' => 1, 'errmsg' => '提交失败')));
            exit;
        }
    }
    include $this->template ('aitimt/cooper');
}
//摄影师注册
if ($op=='recruit'){
    $pagetitle = '注册摄影师';
    if ($_W['ispost']) {
        $uid = (int)$_W['member']['uid'];
        $time=date('Y-m-d');
        $account_api = WeAccount::create();
        $pic=(string)$_GPC['picnum'];
        $temp=[];
        $mediaArr = explode(',',$pic);
        foreach ($mediaArr as $value){
            if($value != null){
                $result = $account_api->downloadMedia($value,$savefile = true);
                array_push($temp,$result);
            }
        }
        $picnum=implode(',',$temp);

        $dataRecruit = [
            'is_confirm' => 1,
            'name' => (string)$_GPC['name'],
            'nickname' => (string)$_GPC['nickname'],
            'mobile' => (string)$_GPC['mobile'],
            'email' => (string)$_GPC['email'],
            'city' => (string)$_GPC['city'],
            'pro' => (string)$_GPC['pro'],
            'camera' => (string)$_GPC['camera'],
            'lens' => (string)$_GPC['lens'],
            'is_pass' => 0,
            'updated_at' => $time,
            'experience' => (string)$_GPC['experience'],
            'picnum' => $picnum,
        ];
        pdo_update('aide_form_data_recruit', $dataRecruit, ['uid' => $uid]);
        die(json_encode(array('errno' => 0, 'msg' => '提交成功')));
    }
    $recruit = pdo_get("aide_form_data_recruit", ['uid'=>(int)$_W['member']['uid']]);
    $recruit['picnum']=explode(",",$recruit['picnum']);
    include $this->template ('aitimt/recruit');
}
//签到表
if ($op=='qiandao'){

    $pagetitle = '个人中心-签到';
    $openid=(string)$_W['fans']['openid'];
    $temp=pdo_fetchall("SELECT content,qian_count FROM " . tablename('aide_activity_qiandao') . " WHERE openid='$openid' AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC LIMIT 1");
    $arr=explode(',',$temp[0]['content']);
    $date=date('Y-m-01');
    $arr=array_unique($arr);
    $qianArr=[];
    foreach ($arr as $item){
        if ($item>=$date){
            $day=substr($item,-2);
            array_push($qianArr,$day);
        }
    }
    $sum=count($arr);
    $qian_count=$temp[0]['qian_count']?$temp[0]['qian_count']:0;
    if ($_W['ispost']){
        //积分变更
        aide_load()->model('credit');
        $credit=5;
        $openid=$_W['fans']['openid'];
        $time=date('Y-m-d');
        if (empty($temp)){
            pdo_insert('aide_activity_qiandao',['uniacid'=>$_W['uniacid'],'openid'=>$openid,'content'=>$time,'qian_count'=>1]);
            $credit_result = credit_update_credit1($_W['member']['uid'], $credit, "爱计时签到：增加" . $credit . '积分', 0);
            $sum++;if ($sum<10){$sum='00'.$sum;}elseif($sum<100){$sum='0'.$sum;}
            die(json_encode(array('errno' => 1,'sum'=>$sum,'qian_count'=>1)));exit;
        }
        if (strpos($temp[0]['content'], $time)===false){
            $data['content'] = $temp[0]['content'].','.$time;
            $lastDay=substr($temp[0]['content'],-10);
            if (strtotime($time)-strtotime($lastDay)>86400){
                $data['qian_count']=1;
            }else{
                $data['qian_count']=$temp[0]['qian_count']+1;
                if ($data['qian_count']==7){
                    $credit=$credit+50;
                }
                if ($data['qian_count']>7){
                    $data['qian_count']=1;
                }
            }
            $result=pdo_update('aide_activity_qiandao',$data,['openid'=>$openid]);
            if ($result>0){
                $credit_result = credit_update_credit1($_W['member']['uid'], $credit, "爱计时签到：增加" . $credit . '积分', 0);
            }
            $sum++;if ($sum<10){$sum='00'.$sum;}elseif($sum<100){$sum='0'.$sum;}
            die(json_encode(array('errno' => 1,'sum'=>$sum,'qian_count'=>$data['qian_count'])));exit;
        }
    }
    if ($sum<10){
        $sum='00'.$sum;
    }elseif($sum<100){
        $sum='0'.$sum;
    }
    $recommendlist = pdo_fetchall("SELECT id,thumb FROM " . tablename('aide_activity') . " WHERE recommend = 1 AND uniacid='{$_W['uniacid']}' AND is_show = 1 ORDER BY id DESC LIMIT 3");
    include $this->template ('aitimt/qiandao');
    }
//常用联系人
if ($op=='mycommon'){
        if (empty($_GPC['edit'])){
            $pagetitle = '个人中心-常用联系人';
            $openid=(string)$_W['fans']['openid'];
            $formList= pdo_fetchall("SELECT * FROM " . tablename('aide_form_data_common') . " WHERE openid='$openid' AND status>0 AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC");
           $status= pdo_fetchall("SELECT * FROM " . tablename('aide_form_data_common') . " WHERE openid='$openid' AND status=2 AND uniacid='{$_W['uniacid']}' ORDER BY  id DESC");
            if (!empty($status)){
                $formItem=$status[0];
            }else{
                $formItem=$formList[0];
            }
           if ($_W['isajax']){
                $fid=(int)$_GPC['fid'];
                foreach ($formList as $value){
                    if ($fid==$value['id']){
                        die(json_encode($value));
                        exit;
                    }
                }
            }
            include $this->template ('aitimt/mycommon');
        }else{
            $pagetitle = '编辑常用联系人';
            $fid=(int)$_GPC['fid'];
            if($fid>0){
                $formItem=pdo_fetchall("SELECT * FROM " . tablename('aide_form_data_common') . " WHERE id=$fid ORDER BY  id DESC LIMIT 1");
                $form=$formItem[0];
            }
            include $this->template ('aitimt/mycommonedit');
        }
}
//设置本人状态
if ($op=='setStatus'){
   $fid=(int)$_GPC['fid'];
   $status=(int)$_GPC['status'];
   $openid=$_W['fans']['openid'];
   pdo_update('aide_form_data_common', ['status'=>1], ['status' => 2,'openid'=>$openid]);
   pdo_update('aide_form_data_common', ['status'=>$status], ['id' => $fid]);
}
//常用联系人编辑
if ($op=='mycommonedit'){
    if ($_W['ispost']) {
        $form = $_GPC['form'];
        $fid = (int)$_GPC['fid'];
        $dataCommon = [
            'realname' => (string)$form['realname'],
            'mobile' => (string)$form['mobile'],
            'uniacid' => $_W['uniacid'],
            'avatar' => $_W['fans']['tag']['avatar'],
            'gender' => (int)$_GPC['gender'],
            'email' => (string)$form['email'],
            'idcard' => (string)$form['idcard'],
            'address' => (string)$form['address'],
            'birthyear' => (string)$_GPC['birth']['year'],
            'birthmonth' => (string)$_GPC['birth']['month'],
            'birthday' => (string)$_GPC['birth']['day'],
            'resideprovince' => (string)$_GPC['reside']['province'],
            'residecity' => (string)$_GPC['reside']['city'],
            'residedist' => (string)$_GPC['reside']['district'],
            'bloodtype' => (string)$_GPC['bloodtype'],
            'education' => (string)$_GPC['education'],
            'openid' => (string)$_W['fans']['openid'],
            'remarks' => (string)$form['remarks'],
            'emerperson' => (string)$form['emerperson'],
            'emerphone' => (string)$form['emerphone'],
            'size' => (string)$form['size'],
        ];
        if (empty($fid)) {
            $dataCommon['status']=1;
            pdo_insert('aide_form_data_common', $dataCommon);
            $fid = pdo_insertid();
            if (!empty($fid)) {
                die(json_encode(array('errno' => 0, 'errmsg' => '新增成功')));
                exit;
            }
        } else {
            pdo_update('aide_form_data_common', $dataCommon, ['id' => $fid]);
            die(json_encode(array('errno' => 0, 'errmsg' => '修改成功')));
            exit;
        }
    }
}
//图片处理
if ($op=='imagesajax'){
        $source = (string)$_GPC['source'];
        if ($source=='common'){ //常用联系人数据库
            $db='aide_form_data_common';
            }elseif ($source=='recruit'){ //注册摄影师数据库
                $db='aide_form_data_recruit';
            }
        $type = (string)$_GPC['type'];
        $fid = (int)$_GPC['fid'];
        $file = $_FILES[$type];
        $file_name = str_pad(date('dHis',time()),8,'9',STR_PAD_LEFT).str_pad(rand(0,99),2,'0',STR_PAD_LEFT);
        $insert_data=[];
        $arr=[];
        //注册摄影师多图上传
        if ($type=='files'){
            foreach ($file['error'] as $key => $error) {
                $data=['name'=>$file['name'][$key],'tmp_name'=>$file['tmp_name'][$key]];
                $upload = file_upload2($_W['uniacid'],$data, $file_name);
                array_push($arr,'/addons/aide_activity' . $upload['path']);
            }
            $path=implode(',',$arr);
            $insert_data['picnum']=$path;
        }else{
            $upload = file_upload2($_W['uniacid'], $file, $file_name);
            $path='/addons/aide_activity' . $upload['path'];
            $insert_data[$type]=$path;
        }
        if(is_error($upload)){
            $out = array(
                'errno' => 1,
                'errmsg' => $upload['message']
            );
            echo json_encode($out);exit;
        }else{
            $insert_data['uniacid'] = $_W['uniacid'];
            $insert_data['uid'] = $_W['member']['uid'];
            if (empty($fid)){
                pdo_insert($db, $insert_data);
                $fid=pdo_insertid();
                $data = array(
                    'fid' => $fid
                );
                $out = array(
                    'errno' => 0,
                    'errmsg' => 'ok',
                    'data' => $data
                );
                echo json_encode($out);exit;
            }else{
                pdo_update($db, $insert_data,['id'=>$fid]);
                $data = array(
                    'fid' => $fid
                );
                $out = array(
                    'errno' => 0,
                    'errmsg' => 'ok',
                    'data' => $data
                );
                echo json_encode($out);exit;
            }

        }
}
//我的积分
if ($op=='mycoin'){
    $pagetitle = '个人中心-我的积分';
    $coin=pdo_get("mc_members", ['uid'=>$_W['member']['uid']]);
    $coinlogs=pdo_fetchall("SELECT num,store_id,createtime FROM " . tablename('mc_credits_record') . " WHERE uid='{$_W['member']['uid']}' AND credittype ='credit1' ORDER BY  id DESC limit 20");
    include $this->template ('aitimt/mycoin');
}
//我的照片
if ($op=='myphoto'){
    $pagetitle = '个人中心-我的照片';
    $uid=$_W['member']['uid'];
    $condition=['uid'=>$uid,'collect'=>0,'good'=>0,'report'=>0];
    pdo_delete('aide_activity_photo_collect',$condition);
    $goodCount = pdo_fetchall("SELECT count(*) num FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND good=1 ORDER BY id ASC");
    $goodIndex = pdo_fetchall("SELECT img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND good=1 AND g_status=1 ORDER BY id ASC limit 1");
    $collectCount = pdo_fetchall("SELECT count(*) num FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND collect=1 ORDER BY id ASC");
    $collectIndex = pdo_fetchall("SELECT img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND collect=1 AND c_status=1 ORDER BY id ASC limit 1");
    $reportCount = pdo_fetchall("SELECT count(*) num FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND report=1 ORDER BY id ASC");
    $reportIndex = pdo_fetchall("SELECT img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND report=1 AND r_status=1 ORDER BY id ASC limit 1");

    include $this->template ('aitimt/myphoto');
}
//我的照片详情
if ($op=='myphotodown'){
    $pagetitle = '个人中心-我的照片详情';
    $type=(string)$_GPC['type'];
    if ($type=='collect'){
        $photoList = pdo_fetchall("SELECT id,img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND collect=1 ORDER BY id ASC");
    }elseif ($type=='good'){
        $photoList = pdo_fetchall("SELECT id,img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND good=1 ORDER BY id ASC");
    }elseif ($type=='report'){
        $photoList = pdo_fetchall("SELECT id,img_src FROM " . tablename('aide_activity_photo_collect') . " WHERE uid=$uid AND report=1 ORDER BY id ASC");
    }
    include $this->template ('aitimt/photodown');
}
//登陆页面
if ($op=='login'){
    $pagetitle = '登陆';
    if ($_W['ispost']){
        if ($_GPC['type']=='weixin'){
            $redirurl=urlencode((string)app_url('home/index/display'));
            $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$_W['account']['key'].'&redirect_uri='.$redirurl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
            $_SESSION["status"]='login';
            die(json_encode(array('errno' => 0,'url'=>$url)));
        }
        if ($_GPC['type']=='phone'){
            $url=app_url('home/index/login1');
            die(json_encode(array('errno' => 0,'url'=>$url)));
        }
    }
    include $this->template ('aitimt/login');
}
//手机登陆页面
if ($op=='login1'){
    $pagetitle = '手机登陆';
    if ($_W['ispost']){
            if ($_GPC['type']=='sms'){
                $mobile = $_GPC['mobile'];
                $code=rand(100000, 999999);
                //发送短信
                //短信模板id,验证码
                $tCode='SMS_205444478';
                aide_load()->classs('alisms');
                $sms = new Sms();
                //测试模式
                $result = $sms->send_verify($mobile, $code,$tCode);
                if ($result) {
                    die(json_encode(array('errno' => 0,'msg'=>'发送成功','code'=>$code)));
                } else {
                    die(json_encode(array('errno' => 1,'msg'=>$result,'code'=>$code)));
                }
            }elseif ($_GPC['type']=='pwd'){
                $mobile=(string)$_GPC['mobile'];
                $pwd=(string)$_GPC['pwd'];
                //数据判断
                $data=['mobile'=>$mobile,'password'=>$pwd,'uid'=>$_W['fans']['uid']];
                $result = pdo_get("mc_members",$data);
                if ($result){
                    $_SESSION["status"]='login';
                    $url=app_url('home/index/display');
                    die(json_encode(array('errno' => 0,'msg'=>'登录成功','url'=>$url)));
                }
                die(json_encode(array('errno' => 1,'msg'=>'账号或密码错误')));
            } else{
                pdo_update('mc_members',['mobile'=>(string)$_GPC['mobile']],['uid'=>$_W['fans']['uid']]);
                $url=app_url('home/index/display');
                $_SESSION["status"]='login';
                //积分变更
                aide_load()->model('credit');
                $credit=5;
                $credit_result = credit_update_credit1($_W['member']['uid'], $credit, "爱计时注册：增加" . $credit . '积分', 3);
                die(json_encode(array('errno' => 0,'msg'=>'登录成功','url'=>$url)));
            }
    }
    include $this->template ('aitimt/login1');
}
//设置密码页面
if ($op=='loginpwd'){
    $pagetitle = '设置密码';
    if ($_W['ispost']){
        pdo_update('mc_members',['password'=>(string)$_GPC['pwd']],['uid'=>$_W['fans']['uid'],]);
        $url=app_url('home/index/my');
        $_SESSION["status"]='login';
        die(json_encode(array('errno' => 0,'msg'=>'设置成功','url'=>$url)));
    }
    include $this->template ('aitimt/loginpwd');
}
//找回密码页面
if ($op=='login2'){
    $pagetitle = '找回密码';
    include $this->template ('aitimt/login2');
}
//退出页面
if ($op=='logout'){
    $pagetitle = '登陆';
    unset($_SESSION['status']);
    if(empty($_W['member']['mobile'])){
        include $this->template ('aitimt/login');
    }else{
        include $this->template ('aitimt/login1');
    }
}
//查询方法
function search($search,$value){
    global $_W;
    $time=date("Y-m-d H:i:s",time());
    //时间查询
    if ($search=='time'){
        if ($value>0){
            $value='%'.date('Y').'-'.$value.'%';
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND endtime LIKE '$value' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }else{
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }
    //状态查询
    }elseif($search=='status'){
        if ($value=='will'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND joinstime>'$time' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }elseif ($value=='now'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND joinstime<'$time' AND joinetime>'$time' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }elseif ($value=='end'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND endtime<'$time' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }elseif($value=='my'){
            $openid=$_W['fans']['openid'];
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND id IN (SELECT activityid FROM `ims_aide_activity_collect` WHERE status=1 AND openid='$openid' ORDER BY  id DESC)");
            return $activityList;
        }elseif ($value=='all'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND is_show=1 ORDER BY id DESC");
            return $activityList;
        }
        //类型查询
    }elseif ($search=='type'){
        if($value=='all'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }elseif ($value=='other'){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND parentid=0 AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }else{
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND parentid=$value AND is_show=1 ORDER BY  id DESC");
            return $activityList;
        }
        //城市查询
    }elseif ($search=='city'){
        $value='%'.$value.'%';
        $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND a_province LIKE '$value' ORDER BY id DESC");
        return $activityList;
        //输入框查询
    }else{
        $value='%'.$value.'%';
        $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND addname LIKE '$value' AND is_show=1 OR uniacid='{$_W['uniacid']}' AND title LIKE '$value' AND is_show=1 ORDER BY id DESC");
        if (empty($activityList)){
            $activityList = pdo_fetchall("SELECT * FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' AND is_show=1 AND parentid IN (SELECT id FROM `ims_aide_category` WHERE name LIKE '$value' ORDER BY id DESC)");
        }
        return $activityList;
    }
}
//号码生成
function numberrule($record){
    global $_W;
    /***************生成号码start***************/
    $nrcondition['uniacid'] = (int)$_W['uniacid'];
    $nrcondition['aid'] = $record['activityid'];
    $nr = pdo_get("aide_activity_number_rule",$nrcondition);
    if($nr){
        if($nr['status']==1 || $nr['status']==2){
            $number_rule = json_decode($nr['number_rule'], true);
            //报名已分配的号码
            $record_condition = array(
                'uniacid' => (int)$_W['uniacid'],
                'activityid' =>$record['activityid'],
            );
            $hd_cy = pdo_getall("aide_activity_records", $record_condition);
            $already_numbers = array(); //已经分配过的号码数组
            foreach($hd_cy as $k => $v){
                $already_numbers[] = $v['cynumber'];
            }
            //规则数组，1全部，2取某字段规则
            if($nr['status'] == 1){
                $rules = $number_rule;
            }elseif($nr['status'] == 2){
                $rules = $number_rule[$record['pid']];
            }
            $cy_number = ''; //新生成的号码
            foreach($rules as $key => $val){
                for($i=$val[0]; $i<=$val[1]; $i++){
                    $cy_number = $key . sprintf("%03d", $i);
                    if(!in_array($cy_number, $already_numbers)){
                        break 2; //该号码未分配过，拿到当前号码，跳出循环
                    }
                }
            }
            if(empty($cy_number)){
                echo 406;exit; //没有号码了，提示错误;
            }
            $data['cynumber'] = $cy_number;

            pdo_update('aide_activity_records',$data,['id'=>$record['id']]);
        }

    }
    /****************生成号码end**************/
}
//图片上传
function file_upload2($uniacid,$file,$attname='', $name = '') {
    load()->func('file');
    $attname = str_pad(date('dHis',time()),8,'9',STR_PAD_LEFT).str_pad(rand(0,99),2,'0',STR_PAD_LEFT);
    if(empty($file)) {
        return error(-1, '没有上传内容');
    }
    $deftype = array('png','jpg');
    $extention = pathinfo($file['name'], PATHINFO_EXTENSION);
    if(!in_array(strtolower($extention), $deftype)) {
        return error(-1, '不允许上传此类文件');
    }
    $result = array();

    if(empty($name) || $name == 'auto') {
        $result['path'] = "/mycommon/".$uniacid."/". date('Ym') . '/';
        mkdirs(MODULE_ROOT . $result['path']);
        do {
            if(empty($attname)){
                $nowname = random(20);
                $filename =  $nowname. ".{$extention}";
            }else{
//                $file['name'] = str_replace(".".$extention,'',$file['name']);
//                $filename = $file['name'] .random(2). ".{$extention}";
                $nowname = $attname;
//                $filename = $attname . ".{$extention}";
                $filename = $attname . ".jpg";
            }
        } while(file_exists(MODULE_ROOT . $result['path'] . $filename));
        $result['path'] .= $filename;
    } else {
        $result['path'] = $name . '.' .$extention;
    }

    if(!file_move($file['tmp_name'], MODULE_ROOT . '/' . $result['path'])) {
        return error(-1, '保存上传文件失败');
    }
    $result['nowname'] = $nowname;
    $result['success'] = true;
    return $result;
}
